# -*- coding: utf-8 -*-
from openerp.osv import fields
from openerp.osv import osv
import pydot

class dftg_design_workflow(osv.osv):
    _name = 'dftg.design.workflow'
    _decription = u'团购项目流程设计图'

    def _get_svg(self, cr, uid, ids, field_name=None, arg=None, context=None):
        res = dict.fromkeys(ids, '')
        for record in self.browse(cr, uid, ids, context=context):
            dot = record['dot'].encode('UTF-8')
            graph = pydot.graph_from_dot_data(dot)
            if isinstance(graph, list):
                svg_string = graph[1].create(prog='dot', format='svg')
            else:
                svg_string = graph.create(prog='dot', format='svg')
            res[record['id']] = svg_string
        return res

    _columns = {
        'dot': fields.char(u'dot定义'),
        'name': fields.char(u'名称'),
        'svg': fields.function(_get_svg, string=u'SVG', type='html', method=True, readonly=True)
    }
